﻿#region Using directives
using System;
using System.ComponentModel;
using System.Linq;
using System.Web.Script.Services;
using System.Web.Security;
using System.Web.Services;

#endregion

namespace ProcolServices {
    /// <summary>
    /// Summary description for UserValidation
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    [ScriptService]
    public class ValidationService : WebService {
        private readonly ProcolDataContext db = new ProcolDataContext();

        [WebMethod]
        public int ValidateUserPasswordCombo(String user, String pass) {
            try {
                User query = (from u in db.Users
                              where
                                      u.Username == user &&
                                      u.Password == FormsAuthentication.HashPasswordForStoringInConfigFile(pass, "SHA1")
                              select u).Single();

                return query.UserId;
            } catch (InvalidOperationException) {
                return -1;
            }
        }
    }
}